<?php

function objectToArray(&$object) {
    $object = (array) $object;
    foreach ($object as $key => $value) {
        if (is_object($value)) {
            objectToArray($value);
            $object[$key] = $value;
        }
        if (is_array($value)) {
            objectToArray($value);
            $object[$key] = $value;
        }
    }
}

function chagearray($sellarray) {
    if (isset($sellarray[1])) {//判断是否为一个分类的一维数组
        foreach ($sellarray as $key => $val) {
            $karray[$val["cid"]] = array("name" => $val["name"], "parent_cid" => $val["parent_cid"]);
        }
        foreach ($karray as $key => $val) {
            foreach ($karray as $k => $v) {
                if ($key == $v["parent_cid"]) {
                    $karray[$key][$k] = $v["name"];
                    unset($karray[$k]);
                }
            }
        }
    } else {
        $karray = array($sellarray["cid"] => array("name" => $sellarray["name"]));
    }
    return $karray;
}

function chagearray2($sellarray) {

    $karray = chagearray($sellarray);

    foreach ($karray as $key => $val) {
        foreach ($val as $k => $v) {
            if (($k != "name") && ($k != "parent_cid")) {
                $karray[$key]["sub"][$k] = $v;
                unset($karray[$key][$k]);
            }
        }
    }
    foreach ($karray as $key => $val) {
        unset($karray[$key]["parent_cid"]);
    }
    return $karray;
}

function generatewanwagarray($shopid) {
    $CI = & get_instance();
    $CI->load->database();
    $sql = "select * from groups where shopid=\"{$shopid}\"";
    $res = $CI->db->query($sql);
    $resarray = $res->result_array();

    foreach ($resarray as $key => $val) {
        $wang_array[$key]['name'] = $val["groupname"];
   $sql = "
        select childaccount.nicekname
 from client,childaccount 
 where client .shopid=\"{$shopid}\" and  client .groupid=\"{$val["roleid"]}\" and client.subid=childaccount.subid 
 and client.shopid=childaccount.shopid
 ";   //      select * from client where shopid=\"{$shopid}\"and groupid={$val["id"]}  
        $res = $CI->db->query($sql);
        $resarray = $res->result_array();

        foreach ($resarray as $k => $v) {
            $wang_array[$key]['group'][$k] = $v["nicekname"];
        }
    }
    return $wang_array;
}

function get_all_child($shopid){
	$CI = & get_instance();
    $CI->load->database();
    $sql = "select * from childaccount where shopid=\"{$shopid}\"";
    $res = $CI->db->query($sql);
    $resarray = $res->result_array();
	return $resarray;
}





function getshopid($nickname) {//登录的时候获的所属的shopid
    $CI = & get_instance();
    $CI->load->library('topclass');

    $resp = $CI->topclass->api_getShopGetRequest("ShopGetRequest", $nickname);
    return $shopid = (string) $resp->shop->sid;
}

function getserverarray($nickname, $sessionley) {//通过topapi获得子账户分组
    $CI = & get_instance();
    $CI->load->library('topclass');
    $resp = $CI->topclass->api_SellercenterSubusersGetRequest("SellercenterSubusersGetRequest", $nickname, $sessionley);
    objectToArray(&$resp);
    return $resp;
}

function frontlogin() {//授权登录后验证一些列处理
    $CI = & get_instance();
    //$CI->load->library('topclass');
    $CI->load->library('parameter', array("get" => $_GET["top_parameters"]));

    $top_parameters_array = $CI->parameter->getParamArray();

    $usernick = $top_parameters_array["visitor_nick"]; //通过链接获得用户昵称        
    $_SESSION["usernick"] = $usernick;
    $_SESSION["shopid"] = getshopid($_SESSION["usernick"]);
    ;
    $top_appkey = $_GET["top_appkey"];
    $top_parameters = $_GET["top_parameters"];
    $top_session = $_GET["top_session"];
    $_SESSION["sessionkey"] = $top_session;
    $CI->config->load('topapiconfig');
    $appsetingarray = $CI->config->item('zwbtj2012');
    $secret = $appsetingarray["behidend"]["appsecck"];
    $top_sign = $_GET["top_sign"];
    return $sign = base64_encode(md5($top_appkey . $top_parameters . $top_session . $secret, true));
}

//function get_tmp_width($Modulename) {//获得模板的宽度
//    $result = explode("_", $Modulename);
//    return $result[0];
//}

function get_tmp_width($Modulename) {//获得模板的宽度
    $result = explode("/", $Modulename);
    $result = explode("_", $result[1]);
    return $result[0];
}

function get_shop_tmpcounts($shopid) {

    $CI = & get_instance();
    $CI->load->database();
    $CI->config->load('infoconfig');
    $width_configarray = $CI->config->item('tmpwidth');
    foreach ($width_configarray as $key => $val) {

        $sql = "select count(id) as counts from res where shopid={$shopid} and width=\"{$val}\" ";
        $res = $CI->db->query($sql);
        $resarray = $res->result_array();

        $returnarray[$val] = $resarray[0]["counts"];
    }
    return $returnarray;
}

function get_tmpres($shopid) {//获得店铺模板的数组
    $CI = & get_instance();
    $CI->load->database();
    $CI->config->load('infoconfig');
    $width_configarray = $CI->config->item('tmpwidth');

    foreach ($width_configarray as $key => $val) {

        $sql = "select * from res where shopid={$shopid} and width=\"{$val}\" ";
        $res = $CI->db->query($sql);
        $resarray = $res->result_array();
        foreach ($resarray as $k => $v) {
            $returnarray[$val][$v["id"]]["name"] = $v["tmpname"];
            $returnarray[$val][$v["id"]]["staute"] = $v["staute"];
            $returnarray[$val][$v["id"]]["time"] = $v["time"];
            $returnarray[$val][$v["id"]]["htmlcontent"] = $v["content"];
            $returnarray[$val][$v["id"]]["tmpath"] = $v["tmpath"];
        }
    }
    if (!isset($returnarray)) {
        return;
    } else {

        return ($returnarray);
    }
}

function decis_last_time() {
    $_sess_time = 14400;
    if (!isset($_SESSION['last_active_time'])) {
        redirect('login', 'location', 301);
    }
    if (($_SESSION['last_active_time'] + $_sess_time) < time()) {
        //$_msg[1] = '错误提示: 登录已经超时.[ERROR02]';
        redirect('login/logout', 'location', 301);
    } else {
        $_SESSION['last_active_time'] = time();
    }
	
}

 function curl($url, $postFields = null)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_FAILONERROR, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		if (is_array($postFields) && 0 < count($postFields))
		{
			$postBodyString = "";
			foreach ($postFields as $k => $v)
			{
				$postBodyString .= "$k=" . urlencode($v) . "&"; 
			}
			unset($k, $v);
			curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);  
 			curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
		}
		$reponse = curl_exec($ch);
                
               
		if (curl_errno($ch)){
			throw new Exception(curl_error($ch),0);
		}
		else{
			 $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
			if (200 !== $httpStatusCode){
				throw new Exception($reponse,$httpStatusCode);
			}
		}
		curl_close($ch);
		return $reponse;
	}



function f100colors(){
	return array('#FF0176','#32305F','#9A22DE','#77BB43','#D0752A','#FF0176',
								'#FFC2BF','#A4DBDE','#8FCBA9','#D9E96D','#D4AFD1','#FF65A3','#49C8CF','#A1D125','#FD4C56',
								'#FBBB11','#D6BA7B','#8798A8','#2A2C41','#613D59','#9970BE','#16499A','#0CA4C9','#670001',
								'#613D59','#FF7F00','#015F47','#F02020','#48AC16','#477E93','#96B7FC','#C0C0C0',
								'#7C79F9','#A45ECD','#FC6D27','#F89327','#7FBA01','#4C9BEB','#317EE7','#998673','#637ABD','#D99898','#7BB1B1',
								'#9F88AD','#8D7F7E','#229B8E','#3C3C3C','#861004','#70C5FF','#D5B18D','#E4EAA0');
	}

